package com.naizhuai.ui.entity;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.naizhuai.util.AESCrypt;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.QRCodeUtil;

import java.io.Serializable;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;

/**
 * 二维码扫描类
 * Created by code_han on 2018/4/18.
 */

public class QRCodeScanBean implements Serializable {

    public static final String QRCode_HIDE_KEY = "genius_han";

    public static final int SCAN_CODE = 1000;
    public static final String SCAN_DATA_KEY = "scan_data_key";

    private String joinType;
    private String userId;
    private String departmentId;
    private String companyId;

    public QRCodeScanBean() {
    }

    public QRCodeScanBean(String joinType, String userId, String departmentId, String companyId) {
        this.joinType = joinType;
        this.userId = userId;
        this.departmentId = departmentId;
        this.companyId = companyId;
    }

    //部门信息
    public QRCodeScanBean(String joinType, String departmentId, String companyId) {
        this.joinType = joinType;
        this.departmentId = departmentId;
        this.companyId = companyId;
    }

    //公司信息
    public QRCodeScanBean(String joinType, String companyId) {
        this.joinType = joinType;
        this.companyId = companyId;
    }


    public String getJoinType() {
        return joinType;
    }

    public void setJoinType(String joinType) {
        this.joinType = joinType;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

    private static String hideEnString(String str) {
        NaizhuAILog.e("str=" + str);
        if (!TextUtils.isEmpty(str))
            try {
                String enStr = AESCrypt.encrypt(QRCode_HIDE_KEY, str);
                NaizhuAILog.e("AESCrypt=" + enStr);
                return enStr;
            } catch (GeneralSecurityException e) {
                return null;
            }
        else
            return null;
    }

    private static String hideDeString(String str) {
        if (!TextUtils.isEmpty(str))
            try {
                return AESCrypt.decrypt(QRCode_HIDE_KEY, str);
            } catch (GeneralSecurityException e) {
                return null;
            }
        else
            return null;
    }

    public String getScanEnMsg() {
        NaizhuAILog.e("加密之前=" + new Gson().toJson(this));
        StringBuilder builder = new StringBuilder();
        if (!TextUtils.isEmpty(joinType))
            builder.append(joinType);
        if (!TextUtils.isEmpty(userId))
            builder.append(QRCodeUtil.DIVIDE_STR).append(userId);
        if (!TextUtils.isEmpty(departmentId))
            builder.append(QRCodeUtil.DIVIDE_STR).append(departmentId);
        if (!TextUtils.isEmpty(companyId))
            builder.append(QRCodeUtil.DIVIDE_STR).append(companyId);
        String encryStr = hideEnString(builder.toString());
        String scanStr = QRCodeUtil.DOWNLOAD_URL + "?" + encryStr;
        NaizhuAILog.e("加密之后=" + scanStr);
        return scanStr;
    }

    public static QRCodeScanBean getScanObject(String urlStr) {
        String[] jsons = urlStr.split(QRCodeUtil.URL_DIVIDE_STR);
        if (jsons[0].equals(QRCodeUtil.DOWNLOAD_URL) && !TextUtils.isEmpty(jsons[1])) {
            String decryStr = hideDeString(jsons[1]);
            if (TextUtils.isEmpty(decryStr))
                return null;
            String[] params = decryStr.split(QRCodeUtil.DIVIDE_STR);
            if (params.length > 1) {
                QRCodeScanBean scanBean = new QRCodeScanBean();
                String key = params[0];
                String param1 = params[1];
                if (TextUtils.isEmpty(key) || TextUtils.isEmpty(param1))
                    return null;
                switch (key) {
                    case QRCodeMsgBean.USER_JOIN_TYPE:
                        scanBean.setJoinType(key);
                        scanBean.setUserId(param1);
                        break;
                    case QRCodeMsgBean.DEPARTMENT_JOIN_TYPE:
                        if (params.length < 3 || TextUtils.isEmpty(params[2]))
                            return null;
                        scanBean.setJoinType(key);
                        scanBean.setCompanyId(params[2]);
                        scanBean.setDepartmentId(param1);
                        break;
                    case QRCodeMsgBean.COMPANY_JOIN_TYPE:
                        scanBean.setJoinType(key);
                        scanBean.setCompanyId(param1);
                        break;
                    default:
                        return null;
                }
                NaizhuAILog.e("解密之后=" + new Gson().toJson(scanBean));
                return scanBean;
            } else
                return null;
        } else
            return null;
    }

}
